package com.dc.controller;

import com.dc.base.controller.BaseController;
import com.dc.base.exception.BusinessException;
import com.dc.base.exception.ResponseCode;
import com.dc.base.pojo.BaseModel;
import com.dc.service.SysRoleService;
import com.dc.vo.SysRoleVo;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping("/role")
public class SysRoleController extends BaseController {
    private static Logger log = Logger.getLogger(SysRoleController.class);

    @Autowired private SysRoleService roleService;

    @RequestMapping("/selectRoleAll")
    @ResponseBody
    @ApiOperation(value="查询所有的角色信息",notes ="查询",httpMethod = "GET")
    @ApiImplicitParam(name = "likeName",value ="模糊查询值"
           ,dataType = "String",paramType = "query")

    public BaseModel selectRoleAll(BaseModel baseModel,
                                  /* @RequestParam(value="likeName",required=false)*/String likeName){
        log.info(baseModel.toString());
     return   roleService.selectRoleAll(baseModel,likeName);
    }

    @RequestMapping("/selectPageRole")
    @ResponseBody
    @ApiOperation(value="分页查询角色信息",notes ="查询",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="current_page",value = "当前页数",required = false),
            @ApiImplicitParam(name="page_size",value = "页数数量",required = false)
    })
    public BaseModel selectPageRole(BaseModel baseModel,@RequestParam(value="baseModel.queryParams.curr_page",defaultValue = "1")int current_page,
                                    @RequestParam(value="baseModel.queryParams.page_size",defaultValue ="10") int page_size ){
        return roleService.selectRoleAll(baseModel,current_page,page_size);
    }

    @ApiOperation(value="新增角色",notes="新增角色和角色的权限",httpMethod = "POST")
    @RequestMapping(value="/insertRoleVo",method = RequestMethod.POST)
    @ResponseBody
    public BaseModel insertRoleVo(@ModelAttribute SysRoleVo roleVo, BaseModel baseModel){
        if(roleVo==null){
            throw new BusinessException(ResponseCode.ADD_ROLE.getCode(),ResponseCode.ADD_ROLE.getMessage());
        }else if(roleVo.getListRolePermission()==null){
            throw new BusinessException(ResponseCode.ADD_ROLEPERMISSION.getCode(),ResponseCode.ADD_ROLEPERMISSION.getMessage());
        }else if (roleVo.getRole().getName() != null && !"".equals(roleVo.getRole().getName())) {
            this.roleService.insertRole(roleVo,baseModel);
            baseModel.setMessage("角色"+roleVo.getRole().getName()+"新增成功");
        }else{
            throw new BusinessException(ResponseCode.ADD_ROLE.getCode(),ResponseCode.ADD_ROLE.getMessage());
        }

        return baseModel;
    }
}
